Javascript: 3 books in 1 : Javascript Basics For Beginners + Javascript Front End Programming + Javascript Back End Programming by Vickler Andy
Author:Vickler, Andy [Vickler, Andy]
Language: eng
Format: epub, pdf
Published: 2021-03-07T16:00:00+00:00
Using React Refs
React has expanded numerous concepts in the web sphere and one of these is declarative views. Before these, we used to call functions to modify the DOM explicitly. As we mentioned at the start of the chapter, views are now declared based on state, and, although functions are still called to alter the state, we have no control over when or if the DOM will change.
This is called inversion of control and, if we didnât use refs, we would lose this imperative nature. Here are some uses where refs can work in your code.
Focus Control
Focus can be achieved in an element programmatically. To do this, focus() must be called on the node instance. DOM will expose this as a function call so creating a ref and doing it manually when needed is the best way around this.
import React from "react";
class InputModal extends React.Component {
constructor(props) {
super(props);
this.state = { value: props.initialValue };
}
onChange = e => {
this.setState({ value: e.target.value });
};
onSubmit = e => {
e.preventDefault();
const { value } = this.state;
const { onSubmit, onClose } = this.props;
onSubmit(value);
onClose();
};
render() {
const { value } = this.state;
return (
<div className="modal--overlay">
<div className="modal">
<h1>Insert a new value</h1>
<form action="?" onSubmit={this.onSubmit}>
<input
type="text"
onChange={this.onChange}
value={value}
/>
<button>Save new value</button>
</form>
</div>
</div>
);
}
}
export default InputModal;
This modal lets the user modify values that have already been set. If the input were on focus at the time the modal opens, the user would have a much better experience. Smooth keyboard transitions between the screens could be enabled, and the first thing to do is get an input reference:
import React, { createRef } from "react";
class InputModal extends React.Component {
constructor(props) {
super(props);
this.inputRef = createRef();
this.state = { value: props.initialValue };
}
onChange = e => {
this.setState({ value: e.target.value });
};
onSubmit = e => {
e.preventDefault();
const { value } = this.state;
const { onSubmit, onClose } = this.props;
onSubmit(value);
onClose();
};
render() {
const { value } = this.state;
return (
<div className="modal--overlay">
<div className="modal">
<h1>Insert a new value</h1>
<form action="?" onSubmit={this.onSubmit}>
<input
ref={this.inputRef}
type="text"
onChange={this.onChange}
value={value}
/ >
<button>Save new value</button>
</form>
</div>
</div>
);
}
}
export default InputModal;
When the modal mounts, focus is imperatively called on the input ref:
import React, { createRef } from "react";
Download
Javascript: 3 books in 1 : Javascript Basics For Beginners + Javascript Front End Programming + Javascript Back End Programming by Vickler Andy.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7808)
Grails in Action by Glen Smith Peter Ledbrook(7719)
Azure Containers Explained by Wesley Haakman & Richard Hooper(6809)
Configuring Windows Server Hybrid Advanced Services Exam Ref AZ-801 by Chris Gill(6807)
Running Windows Containers on AWS by Marcio Morales(6326)
Kotlin in Action by Dmitry Jemerov(5089)
Microsoft 365 Identity and Services Exam Guide MS-100 by Aaron Guilmette(5053)
Combating Crime on the Dark Web by Nearchos Nearchou(4625)
Microsoft Cybersecurity Architect Exam Ref SC-100 by Dwayne Natwick(4577)
Management Strategies for the Cloud Revolution: How Cloud Computing Is Transforming Business and Why You Can't Afford to Be Left Behind by Charles Babcock(4437)
The Ruby Workshop by Akshat Paul Peter Philips Dániel Szabó and Cheyne Wallace(4316)
The Age of Surveillance Capitalism by Shoshana Zuboff(3977)
Python for Security and Networking - Third Edition by José Manuel Ortega(3877)
The Ultimate Docker Container Book by Schenker Gabriel N.;(3535)
Learn Windows PowerShell in a Month of Lunches by Don Jones(3528)
Learn Wireshark by Lisa Bock(3495)
Mastering Python for Networking and Security by José Manuel Ortega(3376)
Mastering Azure Security by Mustafa Toroman and Tom Janetscheck(3353)
Blockchain Basics by Daniel Drescher(3322)
